package entity.Card;

import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import entity.GameClickableObj;
import entity.Pecies;
import control.ItemController;

public class Item extends GameClickableObj implements Cloneable{
	public static Vector<Item> allItemCard = new Vector<Item>();
	private ItemController itemControl = new ItemController();
	private int effectID;
	private String content;
	private String itemName;
	private int roundUse=0;
	private static ImageIcon icon;
	private boolean isChinese;
	private Pecies owner = null;
	public Item(Integer ID,String content, String title,String img,boolean isChinese){
		super(img);
		this.effectID = ID;
		this.content = content;
		this.itemName = title;
		this.isChinese = isChinese;
		allItemCard.add(this);
		icon =new ImageIcon(this.getClass().getResource("/images/bg/tool.PNG"));
	}

	public static int getCardSize() {
		int i = allItemCard.size();
		return i;
	}
	public static Item getItemCard(int index) {
		return allItemCard.get(index);
	}
	
	public String getContent(){
		return this.content;
	}
	public String getItemName(){
		return this.itemName;
	}
	public int getEffectID(){
		return this.effectID;
	}
	public ImageIcon getImageIcon(){
		return Item.icon;
	}
	
	public void setOwner(Pecies owner){
		this.owner = owner;
	}
	
	public void setRoundUse(int value){
		if(value == 0)
			owner.setItemUsing(null);
		else
			this.roundUse = value;
	}
	public int getRoundUse(){
		return roundUse;
	}
	public boolean isRoundFinish(){
		if(roundUse == 0)
			return true;
		return false;
	}
	public Item clone()
    {
        try
        {	
        	
            return (Item)super.clone();
        }
        catch( CloneNotSupportedException e )
        {
            return null;
        }
    } 
	public void setMouseListenerIsActive(boolean isActive) {
	}
	@Override
	public void mouseClicked(MouseEvent e){
			if(e.getX()>this.getX() && e.getX() < this.getX()+30 &&
					e.getY()>this.getY() && e.getY() < this.getY()+30
			){
				if(owner.isItemUsing() && this.effectID!=2){
			    	JOptionPane.showMessageDialog(null, "You are using Item card" , "Item Anncounment",JOptionPane.WARNING_MESSAGE,null);	
				}
				else{
					String question;
					if(isChinese)
						question = "use?";
					else
						question = "Do you need to use this item?";
							
					Object[] message = {this.itemName,this.content,question};
					int res = JOptionPane.showConfirmDialog(null, message,this.itemName,JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE,null);
					if(res == JOptionPane.YES_OPTION){
						switch(this.effectID){
							case 1:
							case 3:
								owner.setItemUsing(this);
								this.setRoundUse(2);
								break;
							case 2:
								Pecies p = itemControl.selectPecies(this.owner);
								if(!p.isItemUsing()){
									owner.setItemUsing(this);
									p.protectByOther(this);
									this.setRoundUse(2);
									owner.setItemUsing(null);
								}
								else{
									JOptionPane.showMessageDialog(null, "Player you selected using Item card" , "Item Anncounment",JOptionPane.WARNING_MESSAGE,null);
								}
								break;
							case 4:
								owner.setItemUsing(this);
								owner.setSelf(5);
								owner.setItemUsing(null);
								break;
						}
						itemControl.doRepaint();
					}
				}
			}
	
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
}
